#include"head.h"
int makeWorker(threadPool_t *pThreadPool){
    for(int i =0;i < pThreadPool->threadNum;++i){
        pthread_create(pThreadPool->tidArr+1,NULL,threadFunc,(void*)pThreadPool);
    }
}
void *threadFunc(void *parg){
    threadPool_t *pThreadPool = (threadPool_t *)parg; //地址传递传入传出参数
    while (1){
        task_t *pTask;//新建一个task来接收队首的任务
        pthread_mutex_lock(&pThreadPool->mutex);
        while (pThreadPool->taskQueue.size == 0){
            pthread_cond_wait(&pThreadPool->cond,&pThreadPool->mutex);//解锁了等一个信号
        }
        puts("GetTask");//来信号了自动上锁
        pTask = pThreadPool->taskQueue.pFront;
        deQueue(&pThreadPool->taskQueue);
        pthread_mutex_unlock(&pThreadPool->mutex);
        printf("netFd = %d\n",pTask->netFd);
        strcpy(pTask->salt,"$6$uw4F3Kne$");//神秘固定盐值
        //接收token，如果是第一次connect token为0
        recv(pTask->netFd,pTask->token,sizeof(pTask->token),0);
        printf("token = %s\n",pTask->token);
        if(strlen(pTask->token)!=0){
            childHandEvent(pTask);
        }else{
            handleEvent(pTask);
        }     
    }
}
int handleEvent(task_t * pTask){
    task_t mypTask = *pTask; //接收下来
    puts("welcome into handEvent!");
    int netFd = mypTask.netFd;
    //询问客户端登录或是注册
    int ret = send(netFd,"登录扣1注册扣2",22,0);
    ERROR_CHECK(ret,-1,"send0");
    char rebuf[20] = {0};
    ret = recv(netFd,rebuf,sizeof(rebuf),0);
    ERROR_CHECK(ret,-1,"recv0");
    char* replyNum = rebuf;
    
    //选择登录
    if(strcmp(replyNum,"1") == 0){
        //puts("select 1");
        while(1){
            ret = recv(netFd, mypTask.usrName, sizeof(mypTask.usrName), 0);
            ERROR_CHECK(ret,-1,"recv1");
            ret = recv(netFd, mypTask.usePwd, sizeof(mypTask.usePwd), 0);
            ERROR_CHECK(ret,-1,"recv2");
            mysqlInit(&mypTask);//建立与数据库的连接
            ret = mysqlLogin(&mypTask);//登录验证
            if(ret == 0){
                send(netFd, "0", 2, 0);
                //登录时用户名与固定盐值制作一个token
                char token[100]={0};
                strcpy(token,crypt(mypTask.usrName,mypTask.salt));
                puts("-----first login-----");
                puts(token);
                //发给客户端token
                send(pTask->netFd,token,strlen(token),0);
                break;
            }else if(ret == -1){
                send(netFd, "-1", 3, 0);
                //循环再次输入
            }
        }
    }else if(strcmp(replyNum,"2") == 0){//选择注册
        mysqlInit(&mypTask);//建立与数据库的连接
        //注册用户进数据库
        ret = recv(netFd,mypTask.usrName,20,0);
        ERROR_CHECK(ret,-1,"recv usrname");
        //puts(mypTask.usrName);
        ret = recv(netFd,mypTask.usePwd,100,0);
        ERROR_CHECK(ret,-1,"recv usePwd");
        mysqlRegister(&mypTask);//用户连接test42这个数据库
        return 0;//注册完只能重新登录
    }else{
        send(netFd,"数字不对！",20,0);
        puts("client send error num,now is exit!");
        return -1;
    }
    while (1){
        bzero(mypTask.buf,sizeof(mypTask.buf));
        int ret = recv(mypTask.netFd,mypTask.buf,sizeof(mypTask.buf),0);
        if(ret == 0){
            puts("对面客户端断开！");
            break;
        }
        sOrderJudge(&mypTask);
    }
    close(netFd);
    return 0;
}

int childHandEvent(task_t *pTask){
    puts("welcome into childHandEvent!");
    task_t mypTask = *pTask; //接收下来
    int netFd = mypTask.netFd;
    recv(netFd,mypTask.usrName,sizeof(mypTask.usrName),0);
    //puts(mypTask.usrName);
    char *token=crypt(mypTask.usrName,mypTask.salt);
    if(strcmp(mypTask.token,token)!=0){
        //密码不对退出
        puts("token错误");
        exit(-1);
    }
    puts("token正确");
    while (1){
        bzero(mypTask.buf,sizeof(mypTask.buf));
        int ret = recv(mypTask.netFd,mypTask.buf,sizeof(mypTask.buf),0);
        if(ret == 0){
            puts("对面客户端断开！");
            break;
        }
        lOrderJudge(&mypTask);
    }
    close(netFd);
    return 0;
}